<?php
class ProductCategory extends AppModel{
    public $useTable  = 'product_category';
    public $lang_code = 'vn';
    
    function add($data = array())
    {
        return($this->save($data));
    }
    
    function del($product_id = null)
    {
        //return $this->deleteAll(array('product_id' => $product_id));
        $sql = "DELETE FROM product_category WHERE product_id = '$product_id'";
        return $this->query($sql);
    }
    
    function get_by_product_id($product_id = null)
    {
        $lang_code = $this->lang_code;
        $sql = "SELECT product_category.*, category_desc.title
                FROM product_category
                LEFT JOIN category_desc ON product_category.category_id = category_desc.category_id 
                AND category_desc.lang_code = '$lang_code'
                WHERE product_category.`product_id` = $product_id";
                
        $data = $this->query($sql);
        
        $product_category = array();
        foreach($data as $value)
        {
            $category_id = $value['product_category']['category_id'];
            $main_category = $value['product_category']['main_category'];
            $title = $value['category_desc']['title'];
            
            $product_category[$category_id]['title'] = $title;
            $product_category[$category_id]['main_category'] = $main_category;
        }
        
        return $product_category;
    }
}
